package com.szy.redstonemcu;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;

public class BuildButton extends GuiButton {
    private McuBaseEntity te;
    private int x=0,y=0,w=36,h=18;

    public BuildButton(int buttonId, int x, int y, String buttonText, McuBaseEntity te) {
        super(buttonId, x, y, buttonText);
        this.x=x;
        this.y=y;
    }
    public BuildButton(int buttonId, int x, int y, int w, int h, String buttonText, McuBaseEntity te) {
        super(buttonId, x, y, w,h,buttonText);
        this.te=te;
        this.x=x;
        this.y=y;
        this.w=w;
        this.h=h;
    }

    @Override
    public boolean mousePressed(Minecraft mc, int mouseX, int mouseY) {
        if(mouseX > x && mouseX < x + w && mouseY > y && mouseY < y + h){
            ItemStack stack=te.getStackInSlot(0);
            Item item=stack.getItem();
            if(item == Items.WRITABLE_BOOK){
                RedstoneMCU.logger.info("book");
                NBTTagCompound nbttagcompound=stack.getTagCompound();
                NBTTagList bookPages = nbttagcompound.getTagList("pages", 8).copy();
                int bookTotalPages = bookPages.tagCount();
                RedstoneMCU.logger.info("page : "+bookTotalPages);
                String s="";
                for(int i=0;i<bookTotalPages;i++){
                    String s2=bookPages.get(i).toString();
                    s+=s2.substring(1,s2.length()-1)+'\n';
                }
                //RedstoneMCU.logger.info("context: \n"+s);
                //RedstoneMCU.simpleNetworkWrapper.sendToServer(new MCUMessage(te.getPos(),s));
//                Assembler assembler=new Assembler(s);
//                if(assembler.assemble()){
//                    RedstoneMCU.logger.info("build successfully");
//                    te.simulator=new CPUSimulator(assembler.getProgram(),256,te.getPeripherals());
//                    te.peripherals.clear();
//                    te.peripherals.add(new GPIO(0x08000000).setIoChangedNotifier(te));
//                }else{
//                    RedstoneMCU.logger.info(assembler.getErrorList());
//                }
            }
            //mc.displayGuiScreen(new EditScreen());

            //mc.displayGuiScreen(new GuiScreenBook(mc.player, stack,false));
        }


        //mc.currentScreen.drawString(te.getDisplayName().getFormattedText(),5,5, Color.darkGray.getRGB());
        //RedstoneMCU.logger.info("click");
        return super.mousePressed(mc, mouseX, mouseY);
    }
}
